package dapps.web.monitor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.DateUtils;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;

import dapps.entity.user.User;
import dapps.service.monitor.MonitorManager;
import dapps.service.monitor.SmsJdbcManager;

@SuppressWarnings("serial")
@Results({ @Result(name = "reload", location = "sms!list.action", type = "redirect"),
	@Result(name = "history", location = "smshistory.action", type = "redirect")
	})
public class SmsAction  extends ActionSupport {

	@Autowired
	private MonitorManager mManager;
	@Autowired
	private SmsJdbcManager smsManager;
	
	// 基本属性

	// private Page<User> page = new Page<User>(15);//每页5条记录
	private List<User> smsuserlist ;
	private Integer usercode;
	private String yearmonth;	
	private Long districtid;
	private String smscontent;
	private int smscount;
	List<Map<String, Object>> smshistory;
	



	// CRUD Action 函数 //

	public List<User> getSmsuserlist() {
		return smsuserlist;
	}

	public String list() throws Exception {
		DateFormat df = new SimpleDateFormat("yyyyMM", java.util.Locale.CHINA);
		if (StringUtils.isBlank(yearmonth))
			yearmonth = df.format(DateUtils.add(DateUtils.getToday(),
					Calendar.MONTH, -1));
		User user = SpringSecurityUtils.getCurrentUser();
		Long udistrictid = user.getDistrict().getId();
		if (udistrictid != null && udistrictid > 0L) {
			districtid = udistrictid;
		}
		if (districtid == null)
			districtid = 0L;
		Integer uc = user.getCode();
		if (uc != null && uc.intValue() > 0) {
			usercode = uc;
		}
		if (districtid != null) {
			smsuserlist = mManager.getSmsPageByDate( yearmonth, usercode,districtid);
			smscontent = smsManager.getHistory(districtid);
		}
		
		return SUCCESS;
	}
	
	public String smshistory() throws Exception{
		smshistory = smsManager.getSmsHistoryList(yearmonth,districtid);
		return "history";
	}
	
	
	public String submitsms() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		String mobileArray[] = request.getParameterValues("choosemobile");
		smscount = smsManager.sendsms(mobileArray, smscontent);
		addActionMessage("发送成功，共发送"+smscount+"条短信。");
		return "reload";
	}
	
	public String savecontent() throws Exception {		
		smsManager.updateHistory(districtid, smscontent);
		addActionMessage("保存成功。");
		return "reload";
	}

	public String getYearmonth() {
		return yearmonth;
	}

	public void setYearmonth(String yearmonth) {
		this.yearmonth = yearmonth;
	}



	public Integer getUsercode() {
		return usercode;
	}

	public void setUsercode(Integer usercode) {
		this.usercode = usercode;
	}


	public Long getDistrictid() {
		return districtid;
	}

	public void setDistrictid(Long districtid) {
		this.districtid = districtid;
	}

	public String getSmscontent() {
		return smscontent;
	}

	public void setSmscontent(String smscontent) {
		this.smscontent = smscontent;
	}

	public int getSmscount() {
		return smscount;
	}

	public List<Map<String, Object>> getSmshistory() {
		return smshistory;
	}

	public void setSmshistory(List<Map<String, Object>> smshistory) {
		this.smshistory = smshistory;
	}
	

}
